/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * 个人信息
 */
import React, {Component} from "react";
import RefreshListView, {RefreshState} from "react-native-refresh-list-view";
import {
    StyleSheet,
    TouchableOpacity,
    Dimensions,
    Image,
    View,
    ToastAndroid,
    ScrollView,
    Button,
    Text,
    TextInput,
    FlatList
} from "react-native";
import api from "../../../api/api";
import color from "../../../widget/color";
import AlteredCell from "../../../widget/homework/AlteredCell";
import RefreshList from "../../../widget/multystateview/RefreshList";
import {DefaultTabBar, ScrollableTabBar} from "react-native-scrollable-tab-view";
import method from "../../../api/method";
import {fetchGetRequest} from "../../../api/NetworkTool";
// import Toast from "react-native-easy-toast";
import EmptyCouponView from "../../../widget/CommonEmptyView";
type Props = {
    navigation: any,
}

type State = {
    typeIndex: number,
    data: Array<Object>,
}
const {width} = Dimensions.get('window')
const {height} = Dimensions.get('window')
export default class AlteredScene extends Component<State,Props> {

    static navigationOptions = ({navigation}: any) => ({
        headerTintColor: 'white',
        title: '我的作业',
        headerStyle: {
            backgroundColor: color.primary,
        },
    });

    constructor(props: Object) {
        super(props)

        this.state = {
            typeIndex: 0,
            data: [],
            refreshState: RefreshState.Idle,
        }
    }

    mapData = (res) => {
        let dataList = res.homework.map((info) => {
            return {
                course_id: info.course_id,
                title: info.lesson_title,
                lesson_no: info.lesson_no,
                course_title: info.course_title,
                lession_id: info.lesson_id,
                start_at: info.start_at,
                end_at: info.end_at,
                teacher: info.teacher,
                homework_end_at: info.homework_end_at,
            }
        })
        return dataList
    }

    componentDidMount() {
        this.requestNetData()
    }

    requestNetData = () => {

        this.setState({refreshState: RefreshState.HeaderRefreshing})

        fetchGetRequest(api.getHomeworkList, method.get, {category: 2})
            .then(res => {
                let dataList = this.mapData(res)
                this.setState({
                    data: dataList,
                    refreshState: RefreshState.Idle,
                })

            })
            .catch(err => {
                this.setState({
                    refreshState: RefreshState.Failure,
                })
            })
    }

    onCellSelected = (info: Object) => {
        // this.props.navigation.navigate('alteredDetailScene', {lession_id: info.lession_id})
        this.props.navigation.navigate('lessonListScene', {course_id: info.course_id, tag: '已批改',category:2, callback: () => {
            this.requestNetData()
        }})
    }

    onsubmit = (info: Object) => {
        // this.props.navigation.navigate('alteredDetailScene', {lession_id: info.lession_id})
        this.props.navigation.navigate('lessonListScene', {course_id: info.course_id, tag: '已批改',category:2, callback: () => {
            this.requestNetData()
        }})
    }

    render() {

        return (
            <View style={{flex:1, backgroundColor:'#f5f5f5'}}>
                <RefreshListView
                    ListHeaderComponent={() => (
                    !this.state.data.length?
                    <EmptyCouponView height={(height-200)} tip={'没有找到已批改的作业'} source={require('../../../../res/img/homework/no_homework.png')}/>
                     : null)
                    }
                    data={this.state.data}
                    renderItem={this.renderCell}
                    keyExtractor={this.keyExtractor}
                    refreshState={this.state.refreshState}
                    onHeaderRefresh={this.requestNetData}
                    onFooterRefresh={this.requestNextPage}
                />
            </View>
        )
    }

    renderCell = (rowData: any) => {
        return (
            <AlteredCell
                info={rowData.item}
                onPress={this.onCellSelected}
                onsubmit={this.onsubmit}
            />
        )
    }

}


